package com.iplant.pdm.serviceImpl.dao.fpc;

import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.pdm.service.po.bop.FPCStepSOP;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

@DBBaseDAO(FPCStepSOP.class)
public class FPCStepSOPDAO extends BaseDAO {
    private static FPCStepSOPDAO Instance = null;

    private static Logger logger = LoggerFactory.getLogger(FPCStepSOPDAO.class);

    private FPCStepSOPDAO() {
        super("");
        this.Init();
    }

    public static FPCStepSOPDAO getInstance() {
        if (Instance == null)
            Instance = new FPCStepSOPDAO();
        return Instance;
    }

    public void FPC_UpdateStepSOP(BMSEmployee wLoginUser, FPCStepSOP wStepSOP, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wStepSOP == null || StringUtils.isEmpty(wStepSOP.Name)) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            FPCStepSOP wStepSOPO = this.FPC_CheckStepSOP(wLoginUser, wStepSOP, wErrorCode);
            if (wStepSOPO.ID > 0) {
                wErrorCode.set(ExceptionTypes.Duplication.getValue());
            }
            if (wErrorCode.Result != 0)
                return;

            Map<String, Object> wParams = new HashMap<String, Object>();

            wParams.put("Version", wStepSOP.Version);
            wParams.put("RoutePartPointID", wStepSOP.RoutePartPointID);

            wParams.put("FilePath", wStepSOP.FilePath);
            wParams.put("FileType", wStepSOP.FileType);
            wParams.put("SourceType", wStepSOP.SourceType);

            wParams.put("Name", wStepSOP.Name);
            wParams.put("Code", wStepSOP.Code);
            wParams.put("Description", wStepSOP.Description);

            wParams.put("EditorID", wLoginUser.getID());
            wParams.put("EditTime", Calendar.getInstance());
            wParams.put("Active", wStepSOP.Active);

            if (wStepSOP.ID > 0) {
                wParams.put("ID", wStepSOP.ID);
                super.Update(super.getTableName("fpc_stepsop"), "ID", wParams);
            } else {
                wParams.put("CreatorID", wLoginUser.getID());
                wParams.put("CreateTime", Calendar.getInstance());
                wStepSOP.ID = super.Insert(super.getTableName("fpc_stepsop"), wParams);
            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_UpdateStepSOP", ex);
        }
    }

    public void FPC_ActiveStepSOP(BMSEmployee wLoginUser, FPCStepSOP wStepSOP, int wActive,
                                  OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        // 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

        try {
            if (wStepSOP == null || wStepSOP.ID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }

            Map<String, Object> wParams = new HashMap<String, Object>();

            wParams.put("ID", wStepSOP.ID);
            wParams.put("EditorID", wLoginUser.getID());
            wParams.put("EditTime", Calendar.getInstance());
            wParams.put("Active", wActive);
            super.Update(super.getTableName("fpc_stepsop"), "ID", wParams);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());

            logger.error("FPC_ActiveStepSOP", ex);
        }
    }

    public List<FPCStepSOP> FPC_QueryStepSOPList(BMSEmployee wLoginUser, int wRouteID, int wRoutePartID,
                                                 List<Integer> wRoutePartPointID, String wRoutePartName, String wPartCode, String wRoutePartPointName,
                                                 String wPartPointCode, String wNameLike, int wActive, Pagination wPagination,
                                                 OutResult<Integer> wErrorCode) {
        return this.FPC_QueryStepSOPList(wLoginUser, -1, "", wRouteID, wRoutePartID, wRoutePartPointID, wRoutePartName,
                wPartCode, wRoutePartPointName, wPartPointCode, wNameLike, wActive, wPagination, wErrorCode);
    }

    private List<FPCStepSOP> FPC_QueryStepSOPList(BMSEmployee wLoginUser, int wID, String wCode, int wRouteID,
                                                  int wRoutePartID, List<Integer> wRoutePartPointID, String wRoutePartName, String wPartCode,
                                                  String wRoutePartPointName, String wPartPointCode, String wNameLike, int wActive, Pagination wPagination,
                                                  OutResult<Integer> wErrorCode) {
        List<FPCStepSOP> wStepSOPList = new ArrayList<FPCStepSOP>();
        wErrorCode.set(0);

        try {
            if (wRoutePartPointID == null)
                wRoutePartPointID = new ArrayList<Integer>();

            wRoutePartPointID.removeIf(p -> p <= 0);
            // Step02:查询
            Map<String, Object> wParams = new HashMap<String, Object>();
            String wSQLText = "";
            wSQLText = StringUtils.Format("Select t.*,t0.Name as RoutePartPointName ,t0.Code as RoutePartPointCode,"
                            + " t0.Version as RoutePartPointVersion,t0.RoutePartID, t1.Name as RoutePartName,t1.Code as  RoutePartCode,"
                            + " t1.Version as RoutePartVersion,t1.RouteID,t1.PartCode, t2.Name as RouteName,"
                            + " t2.Code as RouteCode,t2.Version as RouteVersion, t3.Name as CreatorName ,"
                            + " t4.Name as EditorName  from {0}.fpc_stepsop t  "
                            + " left join {0}.fpc_routepartpoint t0 on t.RoutePartPointID=t0.ID "
                            + " left join {0}.fpc_routepart t1 on t0.RoutePartID=t1.ID "
                            + " left join {0}.fpc_route t2 on t1.RouteID=t2.ID "
                            + " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
                            + " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
                            + " where (:ID<=0 or  t.ID=:ID) and (:Code =''''  or  t.Code=:Code)"
                            + " and (:RouteID<=0 or  t1.RouteID=:RouteID) and (:RoutePartID<=0 or  t0.RoutePartID=:RoutePartID) "
                            + " and (:PartCode =''''  or  t1.PartCode=:PartCode) "
                            + " and (:RoutePartName =''''  or  t1.Name=:RoutePartName) "
                            + " and (:PartPointCode =''''  or  t0.PartPointCode=:PartPointCode) "
                            + " and (:RoutePartPointName =''''  or  t0.Name=:RoutePartPointName) "
                            + " AND (:Active<0 or  t.Active=:Active) AND (:NameLike = '''' or  t.Name like :NameLike)  "
                            + " AND (:RoutePartPointID = '''' or  t.RoutePartPointID IN ({1}) ) group by t.ID ",
                    super.getDBName(),
                    wRoutePartPointID.size() > 0 ? StringUtils.Join(",", wRoutePartPointID) : "0");

            wSQLText = this.DMLChange(wSQLText);

            wParams.put("ID", wID);
            wParams.put("Code", wCode);
            wParams.put("RouteID", wRouteID);
            wParams.put("RoutePartID", wRoutePartID);
            wParams.put("PartCode", wPartCode);
            wParams.put("RoutePartName", wRoutePartName);
            wParams.put("PartPointCode", wPartPointCode);
            wParams.put("RoutePartPointName", wRoutePartPointName);
            wParams.put("RoutePartPointID", StringUtils.Join(",", wRoutePartPointID));
            wParams.put("Active", wActive);
            wParams.put("NameLike", StringUtils.isEmpty(wNameLike) ? "" : ("%" + wNameLike + "%"));

            List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams, wPagination);

            FPCStepSOP wStepSOP = null;
            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                wStepSOP = new FPCStepSOP();
                wStepSOP.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
                wStepSOP.Code = StringUtils.parseString(wSqlDataReader.get("Code"));
                wStepSOP.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
                wStepSOP.Version = StringUtils.parseString(wSqlDataReader.get("Version"));
                wStepSOP.RoutePartPointID = StringUtils.parseInt(wSqlDataReader.get("RoutePartPointID"));
                wStepSOP.RoutePartPointCode = StringUtils.parseString(wSqlDataReader.get("RoutePartPointCode"));
                wStepSOP.RoutePartPointName = StringUtils.parseString(wSqlDataReader.get("RoutePartPointName"));
                wStepSOP.RoutePartPointVersion = StringUtils.parseString(wSqlDataReader.get("RoutePartPointVersion"));
                wStepSOP.RoutePartID = StringUtils.parseInt(wSqlDataReader.get("RoutePartID"));
                wStepSOP.RoutePartCode = StringUtils.parseString(wSqlDataReader.get("RoutePartCode"));
                wStepSOP.RoutePartName = StringUtils.parseString(wSqlDataReader.get("RoutePartName"));
                wStepSOP.RoutePartVersion = StringUtils.parseString(wSqlDataReader.get("RoutePartVersion"));
                wStepSOP.RouteID = StringUtils.parseInt(wSqlDataReader.get("RouteID"));
                wStepSOP.RouteName = StringUtils.parseString(wSqlDataReader.get("RouteName"));
                wStepSOP.RouteCode = StringUtils.parseString(wSqlDataReader.get("RouteCode"));
                wStepSOP.RouteVersion = StringUtils.parseString(wSqlDataReader.get("RouteVersion"));
                wStepSOP.FilePath = StringUtils.parseString(wSqlDataReader.get("FilePath"));
                wStepSOP.FileType = StringUtils.parseInt(wSqlDataReader.get("FileType"));
                wStepSOP.SourceType = StringUtils.parseInt(wSqlDataReader.get("SourceType"));

                wStepSOP.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));
                wStepSOP.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
                wStepSOP.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
                wStepSOP.CreatorName = StringUtils.parseString(wSqlDataReader.get("CreatorName"));
                wStepSOP.EditorName = StringUtils.parseString(wSqlDataReader.get("EditorName"));
                wStepSOP.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
                wStepSOP.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
                wStepSOP.Description = StringUtils.parseString(wSqlDataReader.get("Description"));

                wStepSOPList.add(wStepSOP);
            }

            // Step03:配置系统
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_QueryStepSOPList", ex);
        }
        return wStepSOPList;
    }

    public FPCStepSOP FPC_QueryStepSOP(BMSEmployee wLoginUser, int wID, String wCode, OutResult<Integer> wErrorCode) {
        FPCStepSOP wResult = new FPCStepSOP();
        wErrorCode.set(0);

        try {
            List<FPCStepSOP> wList = null;
            if (wID > 0) {
                wList = this.FPC_QueryStepSOPList(wLoginUser, wID, "", -1, -1, null, "", "", "", "", "", -1,
                        Pagination.getDefault(), wErrorCode);

            } else if (StringUtils.isNotEmpty(wCode)) {
                wList = this.FPC_QueryStepSOPList(wLoginUser, -1, wCode, -1, -1, null, "", "", "", "", "", -1,
                        Pagination.getDefault(), wErrorCode);
            }
            if (wList != null && wList.size() > 0) {
                wResult = wList.get(0);
            }
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_QueryStepSOPByID", ex);
        }
        return wResult;
    }

    public List<FPCStepSOP> FPC_QueryStepSOPList(BMSEmployee wLoginUser, int wRoutePartPointID, String wNameLike,
                                                 OutResult<Integer> wErrorCode) {
        List<FPCStepSOP> wResult = new ArrayList<FPCStepSOP>();
        wErrorCode.set(0);

        try {
            if (wRoutePartPointID <= 0)
                return wResult;

            wResult = this.FPC_QueryStepSOPList(wLoginUser, -1, "", -1, -1,
                    StringUtils.parseListArgs(wRoutePartPointID), "", "", "", "", wNameLike, -1,
                    Pagination.getDefault(), wErrorCode);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_QueryStepSOPList", ex);
        }
        return wResult;
    }

    public List<FPCStepSOP> FPC_QueryStepSOPList(BMSEmployee wLoginUser, int wRoutePartID, String wRoutePartPointName,
                                                 String wPartPointCode, String wNameLike, OutResult<Integer> wErrorCode) {
        List<FPCStepSOP> wResult = new ArrayList<FPCStepSOP>();
        wErrorCode.set(0);

        try {
            if (wRoutePartID <= 0
                    || (StringUtils.isNotEmpty(wRoutePartPointName) && StringUtils.isNotEmpty(wPartPointCode)))
                return wResult;

            wResult = this.FPC_QueryStepSOPList(wLoginUser, -1, "", -1, wRoutePartID, null, "", "", wRoutePartPointName,
                    wPartPointCode, "", -1, Pagination.getDefault(), wErrorCode);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_QueryStepSOPList", ex);
        }
        return wResult;
    }

    public List<FPCStepSOP> FPC_QueryStepSOPList(BMSEmployee wLoginUser, int wRouteID, String wRoutePartName,
                                                 String wPartCode, String wRoutePartPointName, String wPartPointCode, String wNameLike,
                                                 OutResult<Integer> wErrorCode) {
        List<FPCStepSOP> wResult = new ArrayList<FPCStepSOP>();
        wErrorCode.set(0);

        try {
            if (wRouteID <= 0 || (StringUtils.isNotEmpty(wRoutePartName) && StringUtils.isNotEmpty(wPartCode))
                    || (StringUtils.isNotEmpty(wRoutePartPointName) && StringUtils.isNotEmpty(wPartPointCode)))
                return wResult;

            wResult = this.FPC_QueryStepSOPList(wLoginUser, -1, "", wRouteID, -1, null, wRoutePartName, wPartCode,
                    wRoutePartPointName, wPartPointCode, wNameLike, -1, Pagination.getDefault(), wErrorCode);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_QueryStepSOPList", ex);
        }
        return wResult;
    }

    public FPCStepSOP FPC_CheckStepSOP(BMSEmployee wLoginUser, FPCStepSOP wFPCStepSOP, OutResult<Integer> wErrorCode) {
        FPCStepSOP wStepSOP = new FPCStepSOP();
        wErrorCode.set(0);

        try {

            // Step0:查询
            Map<String, Object> wParams = new HashMap<String, Object>();
            String wSQLText = StringUtils.Format(
                    "Select * from {0}.fpc_stepsop where ID!=:ID "
                            + " and ( Code=:Code or  (RoutePartPointID=:RoutePartPointID and Name =:Name   ))  ",
                    super.getDBName());
            wParams.clear();
            wParams.put("ID", wFPCStepSOP.ID);
            wParams.put("Name", wFPCStepSOP.Name);
            wParams.put("Code", wFPCStepSOP.Code);
            wParams.put("RoutePartPointID", wFPCStepSOP.RoutePartPointID);

            wSQLText = this.DMLChange(wSQLText);
            List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams);
            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                wStepSOP.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
                wStepSOP.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
                wStepSOP.Code = StringUtils.parseString(wSqlDataReader.get("Code"));
                wStepSOP.FilePath = StringUtils.parseString(wSqlDataReader.get("FilePath"));

                wStepSOP.FileType = StringUtils.parseInt(wSqlDataReader.get("FileType"));
                wStepSOP.SourceType = StringUtils.parseInt(wSqlDataReader.get("SourceType"));
                wStepSOP.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
                wStepSOP.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
                wStepSOP.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
                wStepSOP.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
                wStepSOP.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_CheckStepSOP", ex);
        }
        return wStepSOP;
    }

}
